package com.sdry.web.controller.zm;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.sdry.model.zm.ZmAgentEntity;
import com.sdry.model.zm.ZmGeneralQueryEntity;
import com.sdry.model.zm.ZmStkmEntity;
import com.sdry.service.zm.ZmAgentService;
import com.sdry.service.zm.ZmStkmService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.zm.MethodLog;

@Controller
@RequestMapping("stkm")
public class ZmStockManagementController {

	@Resource
	ZmStkmService zmStkmService;

	@RequestMapping("toStkm")
	public ModelAndView toStkm() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("zm/stockManagement");
		return mav;
	}
	/**
	 * 新增
	 * @param stkmEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addStkm")
	public String addStkm(ZmStkmEntity zmStkmEntity) {
		if (zmStkmService.addStkm(zmStkmEntity)) {
			return "1";
		}
		return "0";
	}
	/**
	 * 	备货
	 * @param stkmId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("doneStkm")
	public String editStkm(Long stkId) {
		List<Long> ids = new ArrayList<Long>();
		ids.add(stkId);
		if (zmStkmService.editStkm(ids)) {
			return "1";
		}
		return "0";
	}
	@ResponseBody
	@RequestMapping("doneStkms")
	public String doneStkms(@RequestParam(value="ids[]")List<Long> ids){
		if (zmStkmService.editStkm(ids)) {
			return "1";
		}
		return "0";
	}
	/**
	 * 删除
	 * @param stkmId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delStkm")
	public String delStkm(Long  stkId){
		if(zmStkmService.delStkm(stkId)){
			return "1";
		}
		return "0";
	}
	/**
	 * 搜索
	 * @param response
	 * @param queryEntity
	 */
	@MethodLog(remarks="获取备货信息列表")
	@RequestMapping("getStkmList")
	public void getStkmList(HttpServletResponse response, ZmGeneralQueryEntity queryEntity) {
		if (queryEntity.getStrWhere() == null) {
			queryEntity.setStrWhere("status = 0 and isdel = 0 and ");
		}
		List<ZmStkmEntity> getStkmList = zmStkmService.getStkmList(queryEntity);
		int count = zmStkmService.countStkmList(queryEntity);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(getStkmList,jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
